package in.muton.edushare.imageProcessor.processingAPI;

import java.awt.Rectangle;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edushare.common.exceptions.EduShareRuntimeException;

import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;


/**
 * <p><strong>NOTE:</strong> This wrapper is not tested. Plus, if you are running on a server make sure that:
 * <ul>
 * 	<li>You have ImageMagick installed on your machine.</li>
 * 	<li>You have the <strong>jmagick.dll in Win, jmagick.so in Unix</strong> file in system path.</li>
 * 	<li>You have set -D variable on server-startup, like this <code>-Djmagick.systemclassloader = no</code></li>
 * </ul>
 * 
 * I have tested JMagick on Windows somewhere else the best configuration is ImageMagick-6.3.9, JMagick-6.3.9
 * </p>
 * 
 * @author Naishe
 *
 */
public class JMagickWrapper implements IProcessingAPIWrapper {

	private static Log log = LogFactory.getLog(JMagickWrapper.class);
	private ImageInfo origInfo;
	private MagickImage image;
	private MagickImage tempImage;

	static{
		Properties p = new Properties(System.getProperties());
		p.put("jmagick.systemclassloader", "no");
		System.setProperties(p);
	}
	
	private MagickImage getTempImage(){
		if(tempImage!=null)
			return image;
		return tempImage;
	}
	
	public void startNewProcessing() {
		this.tempImage = null;

	}
	
	public void loadFileFromPath(String absPath) {
		try {
			origInfo = new ImageInfo(absPath);
			image = new MagickImage(origInfo);
		} catch (MagickException e) {
			log.fatal("Unable to load file from disk.",e);
			throw new EduShareRuntimeException("Unable to load file from disk");
		}

	}

	public int getOrigWidth() {
		try {
			return image.getDimension().width;
		} catch (MagickException e) {
			log.fatal("Error during getting width.",e);
			throw new EduShareRuntimeException("Error during getting width.");
		}
	}

	public int getOrigHeight() {
		try {
			return image.getDimension().width;
		} catch (MagickException e) {
			log.fatal("Error during getting Height.",e);
			throw new EduShareRuntimeException("Error during getting Height.");
		}
	}

	public void scaleImage(int finalWidth, int finalHeight) {
		try {
			tempImage = getTempImage().scaleImage(finalWidth, finalHeight);
		} catch (MagickException e) {
			log.fatal("Error during scaling.",e);
			throw new EduShareRuntimeException("Error during scaling.");
		}

	}

	public void cropImage(int cropX, int cropY, int cropRectWidth, int cropRectHeight) {
		Rectangle cropRect = new Rectangle(cropX, cropY, cropRectWidth, cropRectHeight);
		try {
			this.tempImage = getTempImage().cropImage(cropRect);
		} catch (MagickException e) {
			log.fatal("Error during cropping.",e);
			throw new EduShareRuntimeException("Error during cropping.");
		}

	}

	public void saveImage(String absNewFilePath) {
		try {
			getTempImage().setFileName(absNewFilePath);
			getTempImage().writeImage(origInfo);
		} catch (MagickException e) {
			log.fatal("Error during saving.",e);
			throw new EduShareRuntimeException("Error during saving.");
		}

	}

	
}
